package com.springcloud.sc.ucpv5.listener;

import com.alibaba.fastjson.JSONObject;
import com.springcloud.sc.comm.core.constant.CommonConstants;
import com.springcloud.sc.ucp.v5.api.entity.UcpConversationRecord;
import com.springcloud.sc.ucp.v5.api.entity.UcpUserDetailInfo;
import com.springcloud.sc.ucpv5.service.UcpConversationRecordService;
import com.springcloud.sc.ucpv5.service.UcpUserDetailInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
public class TopicListener {

/*    @Autowired
    private SimpMessageSendingOperations simpMessageSendingOperations;*/

    @Autowired
    private UcpUserDetailInfoService ucpUserDetailInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UcpConversationRecordService ucpConversationRecordService;

    @JmsListener(destination= "UrgencyVoiceMqMessage", containerFactory = "jmsListenerContainerTopic")
    public void receive(String receive){
//        System.out.println("TopicListener: 收到一条信息: " + receive );
        //给在线用户发送通知
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            public void run() {
                System.out.println("run: 收到一条信息: " + receive );
            }
        });
        executor.shutdown();
    }

    @JmsListener(destination= "UserStatusNotify", containerFactory = "jmsListenerContainerTopic")
    public void receiveUserStatusNotify(String receive){
        //给在线用户发送通知
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            public void run() {
                System.out.println("run: 收到一条信息: " + receive );
                JSONObject json = JSONObject.parseObject( receive );

                String nodeId =json.get("nodeid").toString();
                String number = json.get("number").toString();
                String userstatus = json.get("userstatus").toString();
                UcpUserDetailInfo ucpUserDetailInfo = ucpUserDetailInfoService.getById( number );
                if (ucpUserDetailInfo==null){
                    ucpUserDetailInfo = new UcpUserDetailInfo();
                    ucpUserDetailInfo.setDN( number );
                    ucpUserDetailInfo.setNodeid( nodeId );
                }
                ucpUserDetailInfo.setUserStatus( userstatus );
                ucpUserDetailInfoService.saveOrUpdate(ucpUserDetailInfo);
                redisTemplate.opsForValue().set("online."+number , ucpUserDetailInfo );
            }
        });
        executor.shutdown();
    }

    @JmsListener(destination= "UcpCallLogNotify", containerFactory = "jmsListenerContainerTopic")
    public void receiveUcpCallLogNotify(String receive){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1,1,0, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        executor.execute(new Runnable() {
            public void run() {
                System.out.println("run: 收到一条信息: " + receive );
                JSONObject json = JSONObject.parseObject( receive );
                String callRefId =json.get("refid")!=null ? json.get("refid").toString() : null;
                String caller = json.get("caller")!=null ? json.get("caller").toString() : null;
                String callee = json.get("callee")!=null ? json.get("callee").toString() : null;
                String grpdn = json.get("grpdn")!=null ? json.get("grpdn").toString() : null;
                String sTime = json.get("stime")!=null ? json.get("stime").toString() : null;
                String aTime = json.get("atime")!=null ? json.get("atime").toString() : null;
                String eTime = json.get("etime")!=null ? json.get("etime").toString() : null;
                String callType = json.get("calltype")!=null ? json.get("calltype").toString() : null;
                String ot = json.get("ot").toString();
                UcpConversationRecord ucpConversationRecord = new UcpConversationRecord();
                ucpConversationRecord.setUcrCalleeNumber( callee );
                ucpConversationRecord.setUcrCallerNumber( caller );
                ucpConversationRecord.setUcrCallRefId( callRefId );
                ucpConversationRecord.setUcrCallType( callType );
                ucpConversationRecord.setUcrGrpdnNumber( grpdn );
                ucpConversationRecord.setUcrOt( ot );

                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                LocalDateTime stime =  LocalDateTime.parse(sTime , dtf );
                ZoneId zone = ZoneId.systemDefault();
                Instant instant = stime.atZone(zone).toInstant();
                ucpConversationRecord.setUcrSTime( Date.from(instant) );
                LocalDateTime atime =  LocalDateTime.parse(aTime , dtf );
                Instant instant2 = atime.atZone(zone).toInstant();
                ucpConversationRecord.setUcrATime( Date.from(instant2) );
                LocalDateTime etime =  LocalDateTime.parse(eTime , dtf );
                Instant instant3 = etime.atZone(zone).toInstant();
                ucpConversationRecord.setUcrETime( Date.from(instant3) );
                ucpConversationRecord.setGmtCreateDate( new Date());
                ucpConversationRecord.setDelFlag(CommonConstants.STATUS_NORMAL);
                ucpConversationRecordService.save( ucpConversationRecord );
            }
        });
        executor.shutdown();
    }


}